// 实体生成工具
function EntityTools() {
    return $.layout({
        id: 'entityTools',
        items: [{
            id: 'entity_tools_tree',
            width: 300,
            top: [$.treeSearch, "|", {
                iconCls: 'fa-search',
                text: '选中检索内容',
                handler: function () {
                    var tree = this.up('jtree'), value = tree.getTreeSearch()
                        .v();
                    if (value != '') {
                        tree.getStore().findBy(function (node) {
                            if (node.get('text').indexOf(value) != -1) {
                                node.set('checked', true);
                            }
                        });
                    }
                }
            }, {
                iconCls: 'fa-remove',
                text: '取消选中',
                handler: function () {
                    var tree = this.up('jtree');
                    tree.getStore().getRoot().cascadeBy(function () {
                        var node = this;
                        node.set('checked', false);
                    });
                }
            }],
            style: $.border('Right'),
            region: 'west',
            url: 'admin/ide/allTable',
            xtype: 'jtree',
            change: [{
                target: 'entityTools_columns_grid',
                targetField: 'tableName'
            }]
        }, {
            top: [{
                itemId: 'text',
                xtype: 'textfield',
                req: true,
                blankText: '请输入包名',
                labelWidth: 90,
                text: '请输入包名'
            }, {
                text: '确定生成实体',
                iconCls: 'fa-clipboard',
                handler: function () {
                    var pack = this.up('jgrid')
                        .getDockedItems('toolbar[dock="top"]')[0]
                        .getComponent('text'), text = pack.v();
                    if (!pack.isValid()) {
                        return;
                    }

                    var treeChecked = $('entity_tools_tree')
                        .getChecked(), tbs = [], p = {};
                    $.each(treeChecked, function (node) {
                        tbs.push(node.get('text'));
                    });

                    if (treeChecked.length > 0) {
                        p.tbs = tbs.join(',');
                        p.pack = text;
                        $.confirm('确定生成【 ' + p.tbs + " 】这些表的实体吗?",
                            function () {
                                $.post('admin/ide/createEntity', p, function (text) {
                                    $.show('实体包' + text + "已经在桌面生成!");
                                });
                            });
                    }
                }
            }],
            region: 'center',
            xtype: 'jgrid',
            autoLoad: false,
            page: false,
            id: 'entityTools_columns_grid',
            url: 'admin/ide/getCols',
            columns: [{
                name: 'columnName',
                text: '列名',
                flex: 1
            }, {
                name: 'columnType',
                text: '列类型'
            }, {
                name: 'remarks',
                flex: 1,
                text: '列注释'
            }, {
                name: 'size',
                text: '列长度'
            }, {
                name: 'nullable',
                text: '允许为空'
            }]
        }]
    })
}